//
//  ZMSetupView.m
//  基本框架
//
//  Created by runone02 on 2018/7/2.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "ZMSetupView.h"
#import "ZMSetupViewCell.h"
@interface ZMSetupView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray * dataArray;
@property (nonatomic, strong) NSArray *iconArray;
@end
@implementation ZMSetupView
static NSString *setupViewCellID = @"ZMSetupViewCell";
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return  self;
}

- (void)setUI{
    self.dataArray = @[@"关于我们",@"服务条款",@"意见反馈",@"评价我们",@"清理缓存"];
    self.iconArray = @[@"AlbumReflashIcon",@"tabbar_more_select",@"AlbumReflashIcon",@"tabbar_work_select",@"tabbar_more_select"];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = RGB(241, 241, 241);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(self.mas_bottom).with.offset(0);
    }];
    
    if ([ZMUserInfo shareUserInfo].isLogin) {
        UIView *view = [[UIView alloc]init];
        view.size = CGSizeMake(kScreenWidth, 60);
        UIButton * loginOutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        loginOutBtn.titleLabel.font = TITLE_FONT(30);
        loginOutBtn.backgroundColor = [UIColor whiteColor];
        [loginOutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [loginOutBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [view addSubview:loginOutBtn];
        [loginOutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(40);
            make.centerY.mas_equalTo(view);
        }];
        [loginOutBtn addTarget:self action:@selector(clickLoginOut) forControlEvents:UIControlEventTouchUpInside];
        _tableView.tableFooterView = view;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZMSetupViewCell *cell = [tableView dequeueReusableCellWithIdentifier:setupViewCellID];
    if(cell == nil){
        cell = [[ZMSetupViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:setupViewCellID];
    }
    cell.nameLabel.text = [self.dataArray objectAtIndex:indexPath.row];
    NSString *img = self.iconArray[indexPath.row];
    
    cell.iconImage.image = IMAGE(img);
    
    if (self.dataArray.count- 1 == indexPath.row) {
        cell.showBottomLine = NO;
    }else{
        cell.showBottomLine = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"第%ld组,第%ld行",(long)indexPath.section,(long)indexPath.row);
}


/**退出登录*/
- (void)clickLoginOut{
    [AVUser logOut];
    [[ZMUserInfo shareUserInfo]loginOut];
    [[NSNotificationCenter defaultCenter] postNotificationName:KLoginStateChangeNotice object:nil];
    [self.viewController.navigationController popViewControllerAnimated:YES];
}
@end
